// pages/index/index.js
const app = getApp()

Page({
  data: {
    categories: ['全部', '分级阅读', '桥梁书', '章节书', '其他'],
    currentCategory: '全部',
    sidebarVisible: false,
    currentSidebarItem: 0,
    banners: [
      {
        title: '红火箭分级绘本',
        desc: '科学分级阅读体系，适合各年龄段儿童'
      },
      {
        title: '牛津树系列',
        desc: '全球最受欢迎的儿童英语分级读物'
      },
      {
        title: '典范英语',
        desc: '新版典范英语，全面提升英语能力'
      }
    ],
    recentBooks: [],
    oxfordBooks: [],
    sidebarItems: [
      { title: '全部', icon: 'icon-book', iconText: '📚' },
      { title: '分级阅读', icon: 'icon-layer', iconText: '📚' },
      { title: '桥梁书', icon: 'icon-bridge', iconText: '🌉' },
      { title: '章节书', icon: 'icon-book-open', iconText: '📖' },
      { title: '其他', icon: 'icon-ellipsis', iconText: '⋯' },
      { title: '阅读计划', icon: 'icon-plus', iconText: '➕' }
    ]
  },

  onLoad() {
    this.loadBooks()
  },

  onShow() {
    // 页面显示时更新数据
    this.loadBooks()
  },

  // 加载书籍数据
  loadBooks() {
    const globalData = app.globalData
    this.setData({
      recentBooks: globalData.books,
      oxfordBooks: globalData.oxfordBooks
    })
  },

  // 切换侧边栏
  toggleSidebar() {
    this.setData({
      sidebarVisible: !this.data.sidebarVisible
    })
  },

  // 关闭侧边栏
  closeSidebar() {
    this.setData({
      sidebarVisible: false
    })
  },

  // 分类标签点击
  onCategoryTap(e) {
    const category = e.currentTarget.dataset.category
    this.setData({
      currentCategory: category
    })
    
    // 根据分类筛选书籍
    this.filterBooksByCategory(category)
  },

  // 根据分类筛选书籍
  filterBooksByCategory(category) {
    const globalData = app.globalData
    let filteredBooks = []
    
    if (category === '全部') {
      filteredBooks = globalData.books
    } else {
      filteredBooks = globalData.books.filter(book => book.category === category)
    }
    
    this.setData({
      recentBooks: filteredBooks
    })
  },

  // 侧边栏项目点击
  onSidebarItemTap(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      currentSidebarItem: index,
      sidebarVisible: false
    })
    
    // 根据侧边栏选择更新分类
    const item = this.data.sidebarItems[index]
    if (item.title !== '阅读计划') {
      this.setData({
        currentCategory: item.title
      })
      this.filterBooksByCategory(item.title)
    } else {
      // 处理阅读计划
      this.createPlan()
    }
  },

  // 书籍点击
  onBookTap(e) {
    const book = e.currentTarget.dataset.book
    wx.navigateTo({
      url: `/pages/detail/detail?id=${book.id}&title=${book.title}`
    })
  },

  // 轮播图点击
  onBannerTap(e) {
    const index = e.currentTarget.dataset.index
    const banner = this.data.banners[index]
    
    wx.showToast({
      title: `查看${banner.title}`,
      icon: 'none'
    })
  },

  // 搜索功能
  onSearch() {
    wx.showToast({
      title: '搜索功能开发中',
      icon: 'none'
    })
  },

  // 创建阅读计划
  createPlan() {
    wx.navigateTo({
      url: '/pages/create-plan/create-plan'
    })
  },

  // 页面分享
  onShareAppMessage() {
    return {
      title: '英文书架 - 儿童英语分级阅读',
      path: '/pages/index/index'
    }
  }
}) 